<template>
	<!-- <el-card>
		<el-form ref="queryForm" :inline="true">
			<el-form-item prop="postCode" label="关键词:">
				<el-input v-model="queryParams.keywords" placeholder="关键词"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button icon="Search" type="primary" @click="handleSearch">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button icon="RefreshRight" @click="handleReset">重置</el-button>
			</el-form-item>
		</el-form>
	</el-card> -->
	<el-card>
		<el-form :inline="true" style="text-align: right" class="anniu">
			<el-form-item v-auth="'M007_2_A'">
				<el-button type="primary" @click="addOrUpdateHandle(true, {}, false)">新增</el-button>
			</el-form-item>
			<el-form-item v-auth="'M007_2_Publish'">
				<el-button type="primary" @click="releaseHandle(true)">发布</el-button>
			</el-form-item>
			<el-form-item v-auth="'M007_2_CancelPublish'">
				<el-button type="primary" @click="releaseHandle(false)">取消发布</el-button>
			</el-form-item>
		</el-form>
		<el-table
			ref="xgtableRef"
			v-loading="state.dataListLoading"
			:data="state.dataList"
			stripe
			element-loading-background="rgba(0, 0, 0, 0.5)"
			element-loading-text="加载中..."
			header-row-class-name="table-header"
			@selection-change="selectionChangeHandle"
		>
			<el-table-column type="selection" width="55" />
			<el-table-column type="index" label="序号" header-align="center" align="center" width="60" prop="index"></el-table-column>
			<el-table-column label="通知发布名称" align="center" prop="noticePublishName" />
			<el-table-column label="发布日期" align="center" prop="noticePublishDate">
				<template #default="scope">
					<span v-if="scope.row.noticePublishDate">{{ scope.row.noticePublishDate.substring(0, 10) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="状态" align="center" prop="publishStatus">
				<template #default="scope">
					{{ Number(scope.row.publishStatus) == 0 ? '未发布' : '已发布' }}
				</template>
			</el-table-column>

			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="120">
				<template #default="scope">
					<el-icon
						v-if="Number(scope.row.publishStatus) == 0"
						v-auth="'M007_2_U'"
						:size="16"
						color="#1491FF"
						title="编辑"
						style="cursor: pointer; margin: 0 6px"
						@click="addOrUpdateHandle(false, scope.row, false)"
					>
						<Edit />
					</el-icon>
					<el-icon :size="16" color="#1491FF" title="查看" style="cursor: pointer; margin: 0 6px" @click="addOrUpdateHandle(false, scope.row, true)">
						<View />
					</el-icon>

					<el-icon
						v-if="Number(scope.row.publishStatus) == 0"
						v-auth="'M007_2_D'"
						:size="16"
						color="#fe6360"
						title="删除"
						style="cursor: pointer; margin: 0 6px"
						@click="deleteHandle(scope.row.id)"
					>
						<Delete />
					</el-icon>
				</template>
			</el-table-column>
		</el-table>

		<el-pagination
			:current-page="state.queryForm.pageNum"
			:page-size="state.queryForm.pageSize"
			:total="state.total"
			layout="total, sizes, prev, pager, next, jumper"
			@size-change="sizeChangeHandle"
			@current-change="currentChangeHandle"
		>
		</el-pagination>
	</el-card>

	<Addnotice ref="noticeUpdateRef" @refresh-data-list="getDataList"></Addnotice>
</template>
<script setup lang="ts">
import { govNoticePublishDelApi, govNoticePublishGbApi, govNoticePublishListApi, govNoticePublishQxfbApi } from '@/api/government/index'
import { ElMessage, ElMessageBox, ElTable } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'
import Addnotice from './components/noticeadd.vue'
const addvisible = ref(false)

const state = reactive({
	queryForm: {
		pageNum: 1,
		pageSize: 10
	},
	total: 1,
	dataListLoading: false,
	dateRanges: '',
	applyStatusList: [],
	keyword: '',
	keyType: '',
	dataList: []
})
onMounted(() => {
	getDataList()
})

const noticeUpdateRef = ref()
const addOrUpdateHandle = (isUpdate: Boolean, row: any, isChakan: Boolean) => {
	noticeUpdateRef.value.init(isUpdate, row, isChakan)
}
const sizeChangeHandle = (val: number) => {
	state.queryForm.pageSize = val
	getDataList()
}
const currentChangeHandle = (val: number) => {
	state.queryForm.pageNum = val
	getDataList()
}
const getDataList = async () => {
	state.dataListLoading = true
	await govNoticePublishListApi(state.queryForm).then(res => {
		state.dataListLoading = false
		state.dataList = res.data.list
		state.queryForm.pageNum = res.data.pageNum
		state.queryForm.pageSize = res.data.pageSize
		state.total = res.data.total
		// console.log(state.dataList)
	})
}
// 删除
const deleteHandle = (id: String | Number) => {
	ElMessageBox.confirm('是否删除?', '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning'
	})
		.then(() => {
			delMethods(id)
		})
		.catch(() => {
			ElMessage({
				type: 'info',
				message: '删除失败'
			})
		})
}
const delMethods = async (id: String | Number) => {
	await govNoticePublishDelApi(id).then(res => {
		ElMessage.success('删除成功')
		getDataList()
	})
}
// 获取选择数据列操作
const xgtableRef = ref<InstanceType<typeof ElTable>>()
const multipleSelection = ref([])
const multiByIds = ref([])
const selectionChangeHandle = val => {
	multipleSelection.value = val
}

const recordByIds = () => {
	multiByIds.value = []
	if (multipleSelection.value && multipleSelection.value.length > 0) {
		multipleSelection.value.forEach(i => {
			multiByIds.value.push(i.id)
		})
	}
}
// 发布 或者 不发布 ，如果存在statusType 则提示，重新选择数据
const releaseStatus = statusType => {
	var linshiArr = [...multipleSelection.value]
	return linshiArr.every(item => Number(item.publishStatus) == statusType)
}
const releaseHandle = (isflag: Boolean) => {
	//recordByIds()
	multiByIds.value = []
	if (multipleSelection.value.length == 0) {
		ElMessage.warning({
			message: '请选择数据',
			duration: 500
		})
		return false
	}
	if (multipleSelection.value && multipleSelection.value.length > 0) {
		multipleSelection.value.forEach(i => {
			multiByIds.value.push(i.id)
		})
	}
	if (isflag) {
		if (releaseStatus(0)) {
			yesReleaseHandle()
		} else {
			ElMessage.warning({
				message: '请重新选择数据',
				duration: 500
			})
		}
	} else {
		if (releaseStatus(1)) {
			noReleaseHandle()
		} else {
			ElMessage.warning({
				message: '请重新选择数据',
				duration: 500
			})
		}
	}
}
const yesReleaseHandle = async () => {
	await govNoticePublishGbApi({ ids: multiByIds.value }).then(res => {
		ElMessage.success('发布成功')
		xgtableRef.value!.clearSelection()
		getDataList()
	})
}
const noReleaseHandle = async () => {
	await govNoticePublishQxfbApi({ ids: multiByIds.value }).then(res => {
		ElMessage.success('取消发布成功')
		xgtableRef.value!.clearSelection()
		getDataList()
	})
}
</script>

<style lang="scss" scoped></style>
